﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace EQual.MainModule.Documents.DetailViews
{
    /// <summary>
    /// Interaction logic for EmployeesDestributionDialog.xaml
    /// </summary>
    public partial class EmployeesDistributionDialog : Window , Employees .IEmployeesSelectionView 
    {
        public EmployeesDistributionDialog()
        {
            InitializeComponent();
        }
        /*
        IList<CheckBox> boxes;
        IList<DataAccesLayer.Employee > selectedEmnployees;

        DocumentsDetailViews.DocumentsDestributionPresenter presenter; 

        public object Model
        {
            get { return selectedEmnployees; }
            set
            {
                selectedEmnployees = value as IList<DataAccesLayer.Employee>;
                SetUpWindow();
                SetUpEmployes(value as IList<DataAccesLayer.Employee >);
                AddEventHandlers();

            }
        }

        private void SetUpEmployes(IList<DataAccesLayer.Employee> iList)
        {
            foreach (DataAccesLayer.Employee empl in iList)
                foreach (CheckBox box in boxes)
                    if (empl.ToString () == box.Tag.ToString ())
                        box.IsChecked = true;
        }

        private void SetUpWindow()
        {
            IEnumerable<DataAccesLayer.Employee> empls = new DataAccesLayer.EQualDataLayerFactory().GetEmployeeAccesLayer().GetEmployees();

            List<DataAccesLayer.Employee> employees = new List<DataAccesLayer.Employee>(empls);

            boxes = new List<CheckBox>();


            StackPanel Column = new StackPanel();
            int rows = 5;
          double     columns = employees.Count / 5;
          columns = Math.Round(columns + 0.5);

            for (int j = 0; j < columns; j++)
            {
                int range = employees.Count - (j) * rows;
                int rowsf = rows;
                if (range >= rows)
                    rowsf = rows;
                else
                    rowsf = range;// +rows;
                StackPanel columnj = GetColumnWithEmployees(employees.GetRange(j * rows, rowsf), rowsf);
                this.dockpanel1 . Children.Add(columnj);
            }

        }

        private StackPanel GetColumnWithEmployees(List<DataAccesLayer.Employee> list,int rows)
        {
            StackPanel                 Column = new StackPanel();
            for (int i = 0; i < rows; i++)
            {
                DataAccesLayer.Employee emp = list[i];

                StackPanel box = new StackPanel();
                //   box.Name = emp.ToString ();

                Label id_l = new Label();
                id_l.Content = emp.ToString();

                CheckBox id_c = new CheckBox();

                // id_c.Name = emp.ToString ();
                id_c.Tag = emp;

                box.Children.Add(id_l);
                box.Children.Add(id_c);
                boxes.Add(id_c);

                Column.Children.Add(box);
            }
            return Column;
        }

        private void AddEventHandlers()
        {
            foreach (CheckBox id_c in boxes)
            {
                id_c.Checked += new RoutedEventHandler(id_c_Checked);
                id_c.Unchecked += new RoutedEventHandler(id_c_Unchecked);
            }
        }

        void id_c_Unchecked(object sender, RoutedEventArgs e)
        {
            CheckBox combo = sender as CheckBox;
            this.ItemDeleted (this ,new Microsoft.Practices.Composite.Events.DataEventArgs<object> (combo .Tag));
        }

        void id_c_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox combo = sender as CheckBox;
            this.ItemAdded  (this ,new Microsoft.Practices.Composite.Events.DataEventArgs<object> (combo .Tag));
        }
        */
        void calcel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        void ok_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {

        }


        #region IListView<object,object> Members


        public event EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>> ItemSelected;

        public event EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>> ItemDeleted;

        public event EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>> ItemAdded;

        #endregion

        #region IListView<object,object> Members

        public object Model
        {
            get
            {
                return EmployeesView.Model;
 
            }
            set
            {
                EmployeesView.Model = value;
            }
        }

        #endregion

        private void EmployeesView_ItemAdded(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
        {
            if (ItemDeleted != null)
                this.ItemAdded(this, new Microsoft.Practices.Composite.Events.DataEventArgs<object>(e.Value));
        }

        private void EmployeesView_ItemDeleted(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
        {
            if (ItemAdded != null)
                this.ItemDeleted (this, new Microsoft.Practices.Composite.Events.DataEventArgs<object>(e.Value));
        }
    }
}